package group.sender;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MessageSender {
    private OutputStream outputStream;
    private PrintWriter printWriter;

    public MessageSender(Socket socket) {
        try {
            this.outputStream = socket.getOutputStream();
            this.printWriter = new PrintWriter(outputStream,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String message){
        if (printWriter != null){
            printWriter.println(message);
            printWriter.flush();
        }
    }

    public void close() throws IOException {
        if (outputStream != null){
            outputStream.close();
        }
        if (printWriter != null){
            printWriter.close();
        }
    }
}